<?php
class Mqiu extends CI_Model
{
	function __construct()
	{
		parent::__construct();
		//$this->tablename = 'ssq2014125';
		$this->tablename = 'ssq';
	}
	
	function get_one($id) {
		$item = new stdClass();
		$query = $this->db->query('select * from '.$this->tablename.' where id = '.$id);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

	function get_all($order='') {
		$item = new stdClass();
		if($order)
			$query = $this->db->query('select * from '.$this->tablename.' order by '.$order.' desc');
		else
			$query = $this->db->query('select * from '.$this->tablename);
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}
		$query->free_result();
		return $item;
	}

	function pagination($uid = 0, $pindex = 0, &$totalcount = 0, $perpage = 10) {
		$item = new stdClass();
		$startnumber = $pindex * $perpage;
		$this->db->limit($perpage, $startnumber);
		$query = $this->db->query('select * from `yun_records` where uk = '.$uid.' order by created desc limit '.$startnumber.','.$perpage);
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}

		$query1 = $this->db->query('select rid from `yun_records` where uk = '.$uid);
		$totalcount = $query1->num_rows() ;
		$query->free_result();
		$query1->free_result();
		return $item;
	}

    function exist_id($uk){
		$item = 0 ;
		$query = $this->db->query('select id from '.$this->tablename.' where id = '.$uk);
		if ($query->num_rows() > 0) {
			$item = 1;
		}
		return $item;
	}

    function get_ids($id,$qiu='red'){
		$item = new stdClass();
		$id = (string)$id ;
		$ids = array() ;
		$query = $this->db->query('select id,red,blue  from '.$this->tablename);
		$item = $query->result() ;
		if($qiu == 'red'){
			foreach($item as $v){
				if(strpos($v->red,$id) !== false){
					$iid = $v->id ;
					$ids[$iid] = $iid;
				}
			}
		}else{
			foreach($item as $v){
				if(strpos($v->blue,$id) !== false){
					$iid = $v->id ;
					$ids[$iid] = $iid;
				}
			}
		}

		return $ids;
	}

    function get_num($id,$qiu='red'){
		$item = new stdClass();
		$num = 0 ;
		$id = (string)$id ;
		$query = $this->db->query('select id,red,blue from '.$this->tablename);
		$item = $query->result() ;
		if($qiu == 'red'){
			foreach($item as $v){
				if(strpos($v->red,$id) !== false){
					$num += 1;
				}
				//$query1 = $this->db->query('select id from '.$this->tablename.' where '.$qiu.' like "%'.$id.'%" and id ='.$v->id);
				/*
				$query1 = $this->db->query('select id from '.$this->tablename.' where instr('.$qiu.','.$id.')>0 and id ='.$v->id);
				if ($query1->num_rows() > 0) {
					$num += 1;
				}
				*/
			}
		}else{
			foreach($item as $v){
				if(strpos($v->blue,$id) !== false){
					$num += 1;
				}
			}
		}

		return $num;
	}

    function insert($arr){
		if(!$this->exist_id($arr['id'])){                                    // 防止重复记录
			$this->db->insert($this->tablename,$arr);
			return  $this->db->insert_id(); 
		}else{
			log_message('error', '-----------------用户--'.$arr['username'].'['.$arr['uid'].']'.' 已存在库里-------------------------------');
			return false;
		}
    }

    function update($id,$arr)
    {
        $this->db->where('id',$id);
        $this->db->update($this->tablename,$arr);
    }

    function delete($id){
        $this->db->where('id',$id);
        $this->db->delete($this->tablename);
    }

}